
_start:
.global _start

/*
.extern main	
.extern __fiq__stack__
.extern __irq__stack__
.extern __svc__stack__
.extern jump_app
*/


/* Debug */
LDR r3, =0x11111122
LDR r4, =__os__scratchpad__
str r3, [r4]

/* store return address and stack of boot loader*/
STR lr, [r4,#8]
STR sp, [r4,#12]

/* debug 
LDR r3, =0x11111133
str r3, [r4]
*/

/* Reset the clock cycle register 
mov r0, #0
mcr p15, 0, r0, c15, c12, 1
*/

/* Initialize processor mode stacks */
mov r1, #0x11	/* FIQ mode stack */
LDR r0, =__fiq__stack__
bl init_stack

/* debug */
LDR r3, =0x11111113
str r3, [r4]

mov r1, #0x12	/* IRQ mode stack */
LDR r0, =__irq__stack__ 
bl init_stack

/* debug */
LDR r3, =0x11111114
str r3, [r4]


mov r1, #0x1B	/* UND mode stack */
LDR r0, =__und__stack__ 
bl init_stack


/* debug */
LDR r3, =0x11111115
str r3, [r4]

mov r1, #0x17	/* Abort mode stack */
LDR r0, =__abr__stack__ 
bl init_stack

/* debug */
LDR r3, =0x11111116
str r3, [r4]

mov r1, #0x13	/* SVC mode stack */
LDR r0, =__svc__stack__ 
bl init_stack


/* debug */
LDR r3, =0x11111117
str r3, [r4]

/* Move to SVC mode and disable interrupts */
mrs r2, cpsr
bic r2, r2, #0xff 	/* Disable interrupts, ARM instruction set, clear mode */
orr r2, r2, #0xD3	/* supervisor mode */
msr cpsr, r2

/* Set interrupt vector to high address in CP15 */
mrc p15, 0, r0, c1, c0, 0
orr r0, r0, #0x2000
mcr p15, 0, r0, c1, c0, 0 


/* debug */
LDR r3, =0x11111118
str r3, [r4]

/* copy interrupt vector code to the vector location in memory */
ldr r0, =_interrupt_vector	/* source address */
ldr r1, =0xFFFF0000		/* target address */
mov r2, #14			/* count */
continue_copy:
ldr r3, [r0], #4
str r3, [r1], #4
sub r2, r2, #1
cmp r2, #0
beq done
b continue_copy
done:

/* Debug only */
ldr r3, =0x22222222
ldr r4, =__os__scratchpad__
str r3, [r4]

/*
mov r0, sp
bl print_dummy
STMFD sp!, {r0-r12}
mov r0, sp
bl print_dummy
*/

bl main

/* return to boot loader (if we ever return) */
ldr r3, =0x33333333
ldr r4, =__os__scratchpad__
str r3, [r4]
ldr lr, [r4,#8]
ldr sp, [r4,#12]	/* Warning: FLY may have overwritten bootloader's stack */
mov pc, lr




init_stack:


mrs r2, cpsr
mov r6,r2	/* backup current mode */
bic r2,r2, #0x3f 
orr r2,r2, r1


orr r2, r2, #0xC0  /* disable FIQ,IRQ by default */

/* debug 
mov r3, #0x1
str r6, [r4]
str r2, [r4,#4]
*/

msr cpsr, r2	/* set desired mode */

/* debug */
mov r3, #0x2
str r3, [r4]

mov sp, r0	/* initialize stack pointer for the desired mode */
msr cpsr, r6	/* move back to original mode */

/* debug */
str r0, [r4]
mov pc,lr

.end




